package com.yunche.yunchebusiness.utils.transpager;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.yunche.yunchebusiness.R;
import com.yunche.yunchebusiness.entity.AccountBookBaseInfo;
import com.yunche.yunchebusiness.utils.PixUtils;
import com.yunche.yunchebusiness.utils.SpannableStringUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;

public class ViewpagerNoRecycleAdapter extends PagerAdapter implements CardAdapter {

    private float mBaseElevation;

    private ArrayList<View> views;
    private Context context;

    public ViewpagerNoRecycleAdapter(Context context, ArrayList<View> views) {
        this.context = context;
        this.views = views;
    }


    @Override
    public float getBaseElevation() {
        return mBaseElevation;
    }

    @Override
    public CardView getCardViewAt(int position) {
        return views.get(position).findViewById(R.id.cardView);
    }

    @Override
    public int getCount() {
        return views.size();
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        Log.i("-----", position + "");


        //处理position。让数组下标落在[0,fragmentList.size)中，防止越界
//        position = position % views.size();

        ViewGroup parent = (ViewGroup) views.get(position).getParent();
        if (parent != null) {
            parent.removeView(views.get(position));
        }

        CardView cardView = views.get(position).findViewById(R.id.cardView);

        if (mBaseElevation == 0) {
            mBaseElevation = cardView.getCardElevation();
        }
        cardView.setMaxCardElevation(mBaseElevation * 8);

        container.addView(views.get(position), 0);

        return views.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(views.get(position));

    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }


    private AtomicInteger dateIndex = new AtomicInteger(-1);

    //    private String nowDateStr = DateUtils.getNow("yyyy-MM");
    public void updateItem(int position, String nowDateStr, int scrollDirection) {

        if (scrollDirection == 1) {
            dateIndex.getAndIncrement();
        } else if (scrollDirection == 2) {
            dateIndex.getAndDecrement();
        } else {
            dateIndex = new AtomicInteger(0);
        }

        int[] selectYearMonth = DateUtils.getLastDate(nowDateStr, dateIndex.get());

        View view = views.get(position % views.size());
        TextView textView = view.findViewById(R.id.tv_month);
        TextView tvDateTemp = view.findViewById(R.id.tvDateTemp);
        tvDateTemp.setText(selectYearMonth[0] + "" + selectYearMonth[1]);
        textView.setText(SpannableStringUtils.getBuilder(DateUtils.getMonthEN(selectYearMonth[1]) + "\n")
                .setForegroundColor(Color.parseColor("#999999"))
                .append(selectYearMonth[1] + "月", context)
                .setBold()
                .setForegroundColor(Color.parseColor("#3C3C3F"))
                .create(context));
        //设置选中的左右卡片月份
        //左边
        view = views.get((position - 1) % views.size());
        textView = view.findViewById(R.id.tv_month);
        int[] leftYearMonth = DateUtils.getLastDate(nowDateStr, dateIndex.get() - 1);
        textView.setText(SpannableStringUtils.getBuilder(DateUtils.getMonthEN(leftYearMonth[1]) + "\n")
                .setForegroundColor(Color.parseColor("#999999"))
                .append(leftYearMonth[1] + "月", context)
                .setBold()
                .setForegroundColor(Color.parseColor("#3C3C3F"))
                .create(context));
        //右边
        view = views.get((position + 1) % views.size());
        textView = view.findViewById(R.id.tv_month);
        int[] rightYearMonth = DateUtils.getLastDate(nowDateStr, dateIndex.get() + 1);
        textView.setText(SpannableStringUtils.getBuilder(DateUtils.getMonthEN(rightYearMonth[1]) + "\n")
                .setForegroundColor(Color.parseColor("#999999"))
                .append(rightYearMonth[1] + "月", context)
                .setBold()
                .setForegroundColor(Color.parseColor("#3C3C3F"))
                .create(context));


        if (dateSelectListener != null) {
            dateSelectListener.dateSelected(position, selectYearMonth);
        }
    }


    public void updateBaseInfo(View view, AccountBookBaseInfo baseInfo) {

        DecimalFormat df = new DecimalFormat("#.00");

        if (baseInfo != null) {//更新账本卡片
            TextView pv_sale_num = view.findViewById(R.id.pv_sale_num);
            TextView tv_sale_num = view.findViewById(R.id.tv_sale_num);
            TextView pv_sale_count = view.findViewById(R.id.pv_sale_count);
            TextView tv_sale_count = view.findViewById(R.id.tv_sale_count);
            TextView pv_sale_back = view.findViewById(R.id.pv_sale_back);
            TextView tv_sale_back = view.findViewById(R.id.tv_sale_back);
            TextView textView = view.findViewById(R.id.tv_month);
            TextView tvDateTemp = view.findViewById(R.id.tvDateTemp);
            String yearStr = baseInfo.getTime().substring(0, 4);
            String monthStr = baseInfo.getTime().substring(4, baseInfo.getTime().length());
            tvDateTemp.setText(baseInfo.getTime());
            textView.setText(SpannableStringUtils.getBuilder(DateUtils.getMonthEN(Integer.parseInt(monthStr)) + "\n")
                    .setForegroundColor(Color.parseColor("#999999"))
                    .append(yearStr + "年" + monthStr + "月", context)
                    .setBold()
                    .setForegroundColor(Color.parseColor("#3C3C3F"))
                    .create(context));
            tv_sale_num.setText(baseInfo.getCount() + "辆");
            String price = "";
            if (baseInfo.getRebate() > 10000) {
                price = Double.parseDouble(df.format(baseInfo.getRebate() / 10000)) + "万元";
            }else{
                price = Double.parseDouble(df.format(baseInfo.getRebate())) + "元";
            }
            tv_sale_count.setText(Double.parseDouble(df.format(baseInfo.getSum() / 10000)) + " 万元");
            tv_sale_back.setText(price);
            setWidth(baseInfo.getCount(), baseInfo.getMaxCount(), pv_sale_num);
            setWidth(baseInfo.getRebate(), baseInfo.getMaxRebate(), pv_sale_back);
            setWidth(baseInfo.getSum(), baseInfo.getMaxSum(), pv_sale_count);
        }
    }

    public void setWidth(double real, double max, TextView view) {
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
        if (max == 0) {
            layoutParams.width = PixUtils.dip2px(context, 14);
        } else {
            int width = real == max ? PixUtils.dip2px(context, 80) : (int) (real / max * PixUtils.dip2px(context, 80));
            if (width == 0) {
                width = PixUtils.dip2px(context, 14);
            } else {
                width += PixUtils.dip2px(context, 14);
            }
            layoutParams.width = width;
        }

        view.setLayoutParams(layoutParams);
    }


    public String getCurrentMonth(int position) {
        View view = views.get(position);
        TextView textView = view.findViewById(R.id.tvDateTemp);
        String month = textView.getText().toString();

        if (month.length() == 5) {
            month = month.substring(0, 4) + "0" + month.substring(4, 5);
        }

        return month;
    }

    private DateSelectListener dateSelectListener;

    public void setDateSelectListener(DateSelectListener dateSelectListener) {
        this.dateSelectListener = dateSelectListener;
    }

    public interface DateSelectListener {
        void dateSelected(int positionTemp, int[] selectYearMonth);
    }

}